import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";

declare var $:any;
@Component({
	selector: 'sa-scheduling-config',
	templateUrl: './scheduling-config.component.html',
	styleUrls:['./scheduling-config.css']
})
export class SchedulingConfigComponent implements OnInit {
	config: any = {
		repairStartDate:'',
		remHistoryData:'',
		remParamData:'',
		mainParam:'',
		appointCompanyId:'',
		appointDeviceId:'',
		filterDeviceId:''
	};
	curPage = 0;//缓存页码
	pageType:string = 'new';
	schedulingId = null;
	submitted = false;
	paramConfigData = [];
	single:any = {
		id:'',
		deviceId:'',
		carNumber:'',
		param:''
	}
	singleType:any;
	validatorOptions = {
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			jobName: {
				validators: {
					notEmpty: {
						message: '请输入任务名称'
					},
					stringLength: {
						max: 30,
						message: '资源类型30字以内'
					}
				}
			},
			beanClass: {
				validators: {
					notEmpty: {
						message: '请输入全限定名称'
					}
				}
			},
			methodName: {
				validators: {
					notEmpty: {
						message: '请输入方法名称'
					}
				}
			},
			cronExpression: {
				validators: {
					notEmpty: {
						message: '请输入corn表达式'
					}
				}
			}
		}

	}
	constructor(private jsonApiService: JsonApiService,private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.schedulingId = queryParams.id;
			this.pageType = queryParams.type;
			this.curPage = queryParams.nowPage;
		});
	}
	
//	提交数据
	submitParam() {
		$('#schedulingForm').bootstrapValidator('validate'); //开启验证
		let isValid = $('#schedulingForm').data('bootstrapValidator').isValid(); //验证是否通过
		if(isValid) {
			this.updata();
		}
	}
	updata(){
		this.config.repairStartDate = $("#repairStartDate").val();
		let reqdata  = 'repairStartDate='+this.config.repairStartDate+'&remHistoryData='+this.config.remHistoryData+'&mainParam='+this.config.mainParam+'&appointCompanyId='+this.config.appointCompanyId+
		'&appointDeviceId='+this.config.appointDeviceId+'&filterDeviceId='+this.config.filterDeviceId+'&id='+this.schedulingId;
		this.jsonApiService.http("job/addScheduleJobInfo.html", reqdata).subscribe(data => {
			this.jsonApiService.message(data.message);
			if(data.success){
				this.router.navigate(['/system-config/scheduling'],{ queryParams: {nowPage:this.curPage}});
			}
		})
	}
	//返回资源列表页
	goBack() {
		this.router.navigate(['/system-config/scheduling'],{ queryParams: {nowPage:this.curPage}});
	}
	//参数数据清零
	resetRemParamData(){
//		console.log('等待清零接口')
	}
	//历史数据清零
	resetRemHistoryData(){
		let reqdata = 'scheduleJobId='+this.schedulingId;
		this.jsonApiService.http("job/clearHistoricalData.html",reqdata).subscribe(data=> {
			this.jsonApiService.message(data.message)
		})
	}
	//取消配置
	configCancel(val){
		val.hide();	
	}
	//个体参数配置
	singleParamConfig(val){
		val.show();
		this.getSingleParamData();
	}
	getSingleParamData(){
		let reqdata = 'scheduleJobId='+this.schedulingId;
		this.jsonApiService.http("job/scheduleJobParamInfo.html",reqdata).subscribe(data=> {
			if(data.success){
				this.paramConfigData = data.data;
			}else{
				this.jsonApiService.message(data.message)
			}
		})
	}
	//清空参数
	resetParam(){
		this.single.id = '';
		this.single.deviceId = '';
		this.single.carNumber = '';
		this.single.param = '';
	}
	//添加个体参数
	addParam(val,type){
		this.singleType = type;
		this.resetParam();
		val.show();
	}
	//关闭添加编辑的个体参数弹窗
	singleParamCancel(val){
		this.resetParam();
		val.hide();
	}
	//添加编辑的个体参数提交
	singleParamSubmit(val){
		var reqdata;
		if(this.singleType == "新增"){
			reqdata = 'deviceId='+this.single.deviceId+'&carNumber='+this.single.carNumber+'&param='+this.single.param+'&scheduleJobId='+this.schedulingId;
		}else if(this.singleType == "编辑"){
			reqdata = 'deviceId='+this.single.deviceId+'&carNumber='+this.single.carNumber+'&param='+this.single.param+'&scheduleJobId='+this.schedulingId+'&id='+this.single.id;
		}
		this.jsonApiService.http("job/addOrUpdScheduleJobParam.html",reqdata).subscribe(data=> {
			this.jsonApiService.message(data.message);
			if(data.success){
				val.hide();
				this.getSingleParamData();
			}
		})
	}
	//编辑个体参数
	updataParam(row,val,type){
		this.singleType = type;
		val.show();
		for(let item in this.single){
			this.single[item] = row[item];
		}
	}
	//删除个体参数
	delParam(row){
		let reqdata = 'id='+row.id;
		this.jsonApiService.http("job/deleteScheduleJobParam.html",reqdata).subscribe(data=> {
			this.jsonApiService.message(data.message);
			if(data.success){
				this.getSingleParamData();
			}
		})
	}
	//初始化日期插件
	initDate(){
		common.initDatePicker("#repairStartDate",0);
	}
	ngOnInit() {
		System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
			()=>{System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{this.initDate();});}
		);
		if(this.pageType === 'config') {
			let reqdata = 'id='+this.schedulingId;
			this.jsonApiService.http("job/scheduleJobInfo.html",reqdata).subscribe(data=> {
				if(data.success){
					for(let item in this.config) {
						this.config[item] = data.data[item];
					}
				}else{
					this.jsonApiService.message(data.message)
				}
			})
		}
	}
}